home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <pwd.h>
- #include "sokoban.h"
-
- extern char *strrchr(), *getlogin(), *getpass();
- extern short readscreen(), play(), outputscore(), getuserlevel(),
- makenewscore(), restoregame(), score();
-
- short scoring = 1;
- short level, packets, savepack, moves, pushes, rows, cols;
- short scorelevel, scoremoves, scorepushes;
- char map[MAXROW + 1][MAXCOL + 1];
- POS ppos;
- char *username, *prgname;
-
- static short optshowscore = 0, optmakescore = 0, optrestore = 0, optlevel = 0;
- static short superuser = 0;
-
- char bitfilepath[1024];
- static short userlevel;
- int optbitmap = 0, optwalls = 1;
-
- main(argc, argv)
- short argc;
- char *argv[];
- {
- short ret, ret2;
- struct passwd *pwd, *getpwuid();
-
- #ifdef VICE
- Authenticate();
- #endif
-
- scorelevel = 0;
- moves = pushes = packets = savepack = 0;
- if ((prgname = strrchr(argv[0], '/')) == NULL)
- prgname = argv[0];
- else
- prgname++;
- if ((pwd = getpwuid(getuid())) == NULL)
- ret = E_NOUSER;
- else {
- username = pwd->pw_name;
- superuser = (strcmp(username, SUPERUSER) == 0);
- if ((ret = checkcmdline(argc, argv)) == 0) {
- if (optshowscore)
- ret = outputscore();
- else if (optmakescore) {
- if (superuser) {
- if ((ret = getpassword()) == 0)
- ret = makenewscore();
- } else
- ret = E_NOSUPER;
- } else if (optrestore) {
- ret = restoregame();
- } else if ((ret = getuserlevel(&userlevel)) == 0) {
- if (optlevel > 0) {
- if (superuser) {
- level = optlevel;
- scoring = 0;
- } else if (userlevel < optlevel)
- ret = E_LEVELTOOHIGH;
- else
- level = optlevel;
- } else
- level = userlevel;
- }
- }
- }
- if (ret == 0) {
- ret = gameloop();
- shutdown_screen();
- }
- errmess(ret);
- if (scorelevel && scoring) {
- ret2 = score();
- errmess(ret2);
- }
- exit(ret);
- }
-
- checkcmdline(argc, argv)
- short argc;
- char *argv[];
- {
- short ret = 0;
- int option = 0;
- int tempwalls = 0;
-
- for (option = 1; option < argc; option++) {
- if (argv[option][0] == '-') {
- switch (argv[option][1]) {
- case 's':
- if (optmakescore || optrestore || optlevel || optbitmap)
- ret = E_USAGE;
- optshowscore = 1;
- break;
- case 'c':
- if (optshowscore || optrestore || optlevel || optbitmap)
- ret = E_USAGE;
- optmakescore = 1;
- break;
- case 'r':
- if (optshowscore || optmakescore || optlevel || optbitmap)
- ret = E_USAGE;
- optrestore = 1;
- break;
- case 'b':
- if (optshowscore || optmakescore || optrestore)
- ret = E_USAGE;
- if (argv[option][2]) {
- strcpy(bitfilepath, argv[option] + 2);
- } else {
- option++;
- strcpy(bitfilepath, argv[option]);
- }
- optbitmap = 1;
- break;
- case 'w':
- if (optshowscore || optmakescore || optrestore)
- ret = E_USAGE;
- tempwalls = 1;
- break;
- default:
- if (optshowscore || optmakescore || optrestore)
- ret = E_USAGE;
- if ((optlevel = atoi(argv[option] + 1)) == 0)
- ret = E_USAGE;
- break;
- }
- } else {
- ret = E_USAGE;
- }
- }
- if (optbitmap && !tempwalls)
- optwalls = 0;
- return (ret);
- }
-
- gameloop()
- {
-
- short ret = 0;
-
- switch (init_screen()) {
- case E_NOBITMAP:
- return E_NOBITMAP;
- break;
-
- case E_NODISPLAY:
- exit(1);
- break;
-
- default:
- break;
- }
- if (!optrestore)
- ret = readscreen();
- while (ret == 0) {
- if ((ret = play()) == 0) {
- level++;
- moves = pushes = packets = savepack = 0;
- ret = readscreen();
- }
- }
- return (ret);
- }
-
- getpassword()
- {
-
- return ((strcmp(getpass("Password: "), PASSWORD) == 0) ? 0 : E_ILLPASSWORD);
- }
-
- char *message[] =
- {
- "illegal error number",
- "cannot open screen file",
- "more than one player position in screen file",
- "illegal char in screen file",
- "no player position in screenfile",
- "too much rows in screen file",
- "too much columns in screenfile",
- "quit the game",
- NULL, /* errmessage deleted */
- "cannot get your username",
- "cannot open savefile",
- "error writing to savefile",
- "cannot stat savefile",
- "error reading savefile",
- "cannot restore, your savefile has been altered",
- "game saved",
- "too much users in score table",
- "cannot open score file",
- "error reading scorefile",
- "error writing scorefile",
- "illegal command line syntax",
- "illegal password",
- "level number too big in command line",
- "only superuser is allowed to make a new score table",
- "cannot find file to restore",
- "cannot find bitmap file",
- "cannot open display",
- "cannot load font",
- "cannot allocate string memory",
- "could not load requested color"
- };
-
- errmess(ret)
- register short ret;
- {
- if (ret != E_ENDGAME) {
- fprintf(stderr, "%s: ", prgname);
- switch (ret) {
- case E_FOPENSCREEN:
- case E_PLAYPOS1:
- case E_ILLCHAR:
- case E_PLAYPOS2:
- case E_TOMUCHROWS:
- case E_TOMUCHCOLS:
- case E_ENDGAME:
- case E_NOUSER:
- case E_FOPENSAVE:
- case E_WRITESAVE:
- case E_STATSAVE:
- case E_READSAVE:
- case E_ALTERSAVE:
- case E_SAVED:
- case E_TOMUCHSE:
- case E_FOPENSCORE:
- case E_READSCORE:
- case E_WRITESCORE:
- case E_USAGE:
- case E_ILLPASSWORD:
- case E_LEVELTOOHIGH:
- case E_NOSUPER:
- case E_NOSAVEFILE:
- case E_NOBITMAP:
- case E_NODISPLAY:
- case E_NOFONT:
- case E_NOMEM:
- case E_NOCOLOR:
- fprintf(stderr, "%s\n", message[ret]);
- break;
- default:
- fprintf(stderr, "%s\n", message[0]);
- break;
- }
- if (ret == E_USAGE)
- usage();
- }
- }
-
- static char *usagestr[] =
- {
- " -c : create new score table (superuser only)\n",
- " -r : restore saved game\n",
- " -s : show score table\n",
- " -<nn> : play this level (<nn> must be greater 0)\n",
- " -b <bitmap_dir>: use alternate bitmaps. See ingame help.\n",
- " implies !-w\n",
- " -w : use fancy walls (default unless you use your\n",
- " own set of bitmaps)\n",
- NULL
- };
-
- usage()
- {
-
- register short i;
-
- fprintf(stderr, "Usage: %s [-{s|c|r|<nn>|b <bitmap_dir>|w}]\n\n", prgname);
- for (i = 0; usagestr[i] != NULL; i++)
- fprintf(stderr, "%s", usagestr[i]);
- }
-